function impred = deploy(net, imlow, bGPU, rf)
% thresh = 90000;
thresh = 90000*2;
if nargin < 4, rf = 20; end

hDeploy = @(x) deploy2(net, x, bGPU);

s = size(imlow);
if s(1)*s(2) <= thresh
    impred = hDeploy(imlow);
    return
end

%%
% v = ceil(size(imlow, 1)/2);
% h = ceil(size(imlow, 2)/2);
v = size(imlow, 1) / 2;
h = size(imlow, 2) / 2;

%%
imTL = imlow(1:v+rf, 1:h+rf, :);
imBL = imlow(v-rf+1:end, 1:h+rf, :); 
imTR = imlow(1:v+rf, h-rf+1:end, :);
imBR = imlow(v-rf+1:end, h-rf+1:end, :);

%%
impredTL = hDeploy(imTL);
impredTL = impredTL(1:v, 1:h, :);

impredBL = hDeploy(imBL);
impredBL = impredBL(rf+1:end, 1:h, :);

impredTR = hDeploy(imTR);
impredTR = impredTR(1:v, rf+1:end, :);

impredBR = hDeploy(imBR);
impredBR = impredBR(rf+1:end, rf+1:end, :);

%%
impredL = cat(1, impredTL, impredBL);
impredR = cat(1, impredTR, impredBR);
impred = cat(2, impredL, impredR);

function Y = deploy2(net, X, bGPU)
X = single(X);
Y = zeros(size(X));
n = size(X, 3);
for i = 1 : n
    fprintf('\tVDSR0: %d/%d\n', i, n);
    x = X(:, :, i);
    if bGPU, x = gpuArray(x); end
    res = vl_simplenn(net, x, []);% {'conserveMemory', true});
    y = x + res(end).x;
    if bGPU, y = gather(y); end
    Y(:, :, i) = y;
end
